package com.chulianyuang.service.impl;

import com.chulianyuang.mapper.ClaimVoucherDetailMapper;
import com.chulianyuang.mapper.ClaimVoucherMapper;
import com.chulianyuang.mapper.EmployeeMapper;
import com.chulianyuang.pojo.ClaimVoucher;
import com.chulianyuang.pojo.ClaimVoucherDetail;
import com.chulianyuang.service.ClaimVoucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ClaimVoucherServiceimpl implements ClaimVoucherService {
    @Autowired
    private ClaimVoucherMapper claimVoucherMapper;
    @Autowired
    private ClaimVoucherDetailMapper claimVoucherDetailMapper;

    @Override
    public List<ClaimVoucher> selectClaimVoucherBySn( ClaimVoucher claimVoucher) {

    return claimVoucherMapper.getClaimVoucher(claimVoucher);
    }


    @Override
    public boolean addClaimVoucher(ClaimVoucher cdv) {

        //添加报销单头
        int a= claimVoucherMapper.addClaimVoucher(cdv);
//        循环添加报销单明细
        if(a>0){

            for(ClaimVoucherDetail cdvd:cdv.getDetailList()){
                cdvd.setMainId(cdv.getId());

                claimVoucherDetailMapper.addClaimVoucherDetail(cdvd);
            }
        }

        return true;
    }

    @Override
    public ClaimVoucher selectClaimVoucherById(int id) {
        return claimVoucherMapper.selectClaimVoucher(id);
    }

    @Override
    public boolean deleteClaimVoucherById(int id) {
        if(claimVoucherMapper.deleteClaimVoucherById(id)>0){
            claimVoucherDetailMapper.deleteClaimVoucherDetailById(id);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateClaimVoucherById(ClaimVoucher cdv) {
        return claimVoucherMapper.updateClaimVoucher(cdv)>0;
    }

    @Override
    public boolean updateClaimVoucher(ClaimVoucher claimVoucher) {
        try {
            System.out.println("Service层：开始更新报销单，ID: " + claimVoucher.getId());

            // 1. 更新报销单头
            int mainResult = claimVoucherMapper.updateClaimVoucherFull(claimVoucher);
            System.out.println("更新主表结果: " + mainResult);

            if (mainResult <= 0) {
                return false;
            }

            // 2. 更新报销单详情（先删除后新增）
            // 删除原有明细
            int deleteCount = claimVoucherDetailMapper.deleteClaimVoucherDetailById(claimVoucher.getId());
            System.out.println("删除原有明细数量: " + deleteCount);

            // 插入新明细
            int insertCount = 0;
            for (ClaimVoucherDetail detail : claimVoucher.getDetailList()) {
                claimVoucherDetailMapper.addClaimVoucherDetail(detail);
                insertCount++;
            }
            System.out.println("插入新明细数量: " + insertCount);

            return true;
        } catch (Exception e) {
            System.out.println("Service层更新异常: " + e.getMessage());
            throw new RuntimeException("更新报销单失败", e);
        }
    }

}
